package com.os.core.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 描述:
 *
 * @author Matt
 * @create 2021-05-30 17:02
 */

//@ControllerAdvice
public class OsExceptionHandler {

    public static final String DEFAULT_ERROR_VIEW = "error";

    //处理所有的异常，主要是异常拦截到的都做处理
    //@ExceptionHandler(value = Exception.class)
    public Object errorHandler(HttpServletRequest req, HttpServletResponse rsp, Exception e) throws Exception{

        System.out.println("------------捕获全局异常------------");
        e.printStackTrace();

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("exception", e);
        modelAndView.addObject("url", req.getRequestURL());
        modelAndView.setViewName(DEFAULT_ERROR_VIEW);

        return modelAndView;

    }
}
